import { queryEnterprise, getAllEnterprise } from '@/api/enterprise'
import { queryBase } from '@/api/base'
import { queryGood_entId } from '@/api/good'
import { queryWarehouse_entId } from '@/api/warehouse'
import { queryCertification_entId } from '@/api/certificate'
import store from '@/store'

export const entMixin = {
  data() {
    return {
      account: store.getters.token,
      role: store.getters.role,
      entOptions: [],
      goodOptions: [],
      objList: [],
      entFlag: false,
      goodFlag: false
    }
  },
  computed: {
    // entFlag() {
    //   return this.entOptions[0].value === -1
    // },
    // goodFlag() {
    //   return this.goodOptions[0].value === -1
    // }
  },
  methods: {
    fetchData() {
      this.listLoading = true
      if (this.role === '0') {
        this.getEntAdmin()
      } else {
        this.getEnt()
      }
    },
    getEntAdmin() {
      getAllEnterprise({
        page: -1,
        limit: -1
      })
        .then(response => {
          this.formatEntData(response)
        })
        .catch(error => {
          console.log(error)
        })
    },
    getEnt() {
      if (this.role === '4') {
        queryEnterprise({
          account: this.account,
          page: -1,
          limit: -1
        })
          .then(response => {
            this.formatEntData(response)
          })
          .catch(error => {
            console.log(error)
          })
      } else {
        queryBase({ account: this.account })
          .then(response => {
            this.formatEntData(response)
          })
          .catch(error => {
            console.log(error)
          })
      }
    },
    formatEntData(response) {
      this.entOptions = response.enterprise.map(item => {
        return {
          label: item.entName,
          value: item.entId
        }
      })
      if (this.entOptions.length === 0) {
        this.entFlag = true
        this.entOptions.push({
          label: '当前账号无所属企业！',
          value: -1
        })
        this.listLoading = false
      } else {
        if (this.type === 'good') {
          this.getGood(0)
        } else if (this.type === 'warehouse') {
          this.getWarehouse(0)
        } else {
          this.getGood(0)
          this.getCertification(0)
        }
      }
      this.temp.entId = this.entOptions[0].value
    },
    getGood(count) {
      if (count === this.entOptions.length) {
        if (this.type === 'certification') {
          this.checkGoodOptions()
        }
        this.listLoading = false
        return
      }
      queryGood_entId({ entId: this.entOptions[count].value })
        .then(response => {
          if (this.type === 'certification') {
            const goods = response.good.map(item => {
              return {
                label: item.name,
                value: item.id
              }
            })
            this.goodOptions.push(...goods)
          } else {
            this.objList.push(...response.good)
          }
          this.getGood(++count)
        })
        .catch(error => {
          console.log(error)
        })
    },
    checkGoodOptions() {
      if (this.goodOptions.length === 0) {
        this.goodFlag = true
        this.goodOptions.push({
          label: '请先添加商品',
          value: -1
        })
      }
    },
    getWarehouse(count) {
      if (count === this.entOptions.length) {
        this.listLoading = false
        return
      }
      queryWarehouse_entId({ entId: this.entOptions[count].value })
        .then(response => {
          this.objList.push(...response.Warehouse)
          this.getWarehouse(++count)
        })
        .catch(error => {
          console.log(error)
        })
    },
    getCertification(count) {
      if (count === this.entOptions.length) {
        this.listLoading = false
        return
      }
      queryCertification_entId({ entId: this.entOptions[count].value })
        .then(response => {
          this.objList.push(...response.Certification)
          this.getCertification(++count)
        })
        .catch(error => {
          console.log(error)
        })
    }
  }
}
